package io;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycy
 * @CreateDate: 2019/3/29 15:51
 */
public class Station {

  public static void main(String[] args) {
    Station Station = new Station();
    //bu提供的站  2630
    Map<String, String> map = Station.readFile();

    //攻略提供的站  3245
    Map<String, String> map2 = Station.readFile2();

    //车站和目的地方对应 2572 + 58
    Map<String, String> map3 = Maps.newHashMap();


    //攻略提供的目的地
    List<String> citys = Lists.newArrayList();
    for (Map.Entry<String, String> entry : map.entrySet()) {
      String station = entry.getKey()+ "站";
      if (!map2.containsKey(station)) {
//        System.out.println(station);   //58
      } else {
        String stationCity = map2.get(station);
//        System.out.println(station + "\t" + stationCity);
        citys.add(stationCity);
        map3.put(station, stationCity);
      }
    }
    //攻略提供的城市
    Map<String, String> cityMap = Station.readFile3();
    for (String city : citys) {
      String[] split = city.split(",");
      boolean flag = false;
      for (String s : split) {
        if (cityMap.containsKey(s.trim())) {
          flag = true;
        }
      }
      if (!flag) {
//        System.out.println(city);
      }

    }

    //
    for (Map.Entry<String, String> stationCity : map3.entrySet()) {
      String key = stationCity.getKey();
      String value = stationCity.getValue();
      String[] citySplit = value.split(",");
      String s = null;
      String district = null;
      for (String city : citySplit) {
        if ((s = cityMap.get(city)) != null) {
          district = city;
          break;
        }
      }
      System.out.println(key + "\t" + district + "\t" + s);
    }


  }

  /**
   * 火车线路 -->  火车站
   *
   * @return
   */
  public Map<String, String> readFile() {
    try {
      List<String> lineStations = FileUtils.readLines(new File("D:\\Users\\yincy\\Desktop\\火车票线路\\火车票线路.txt"), "UTF-8");
//      for (String s : lineStations) {
//        System.out.println(s);
//      }
      System.out.println("读取了" + lineStations.size());
      //拆成站
      Map<String, String> station = Maps.newHashMap();
      for (String lineStation : lineStations) {
        String[] split = lineStation.split("_");
        String trim = split[0].replaceAll(" ", "").trim();
        station.put(trim, trim);
        String trim1 = split[1].replaceAll(" ", "").trim();
        station.put(trim1, trim1);
      }

      return station;
    } catch (IOException e) {
      e.printStackTrace();
    }

    return null;
  }

  /**
   * 站和城市对应关系
   *
   * @return
   */
  public Map<String, String> readFile2() {
    try {
      List<String> lineStations = FileUtils.readLines(new File("D:\\Users\\yincy\\Desktop\\火车票线路\\车站和目的地对应关系.txt"), "UTF-8");
      Map<String, String> stationMap = Maps.newHashMap();
      System.out.println("读取了" + lineStations.size());
      int i = 0;
      for (String lineStation : lineStations) {
        String[] split = lineStation.split("\t");
//        System.out.println(++i);
        if(stationMap.containsKey(split[0])){
//          System.out.println(split[0]);
        }
        String trim = split[0].replaceAll(" ", "").trim();
        String trim1 = split[1].replaceAll(" ", "").trim();
        stationMap.put(trim, trim1);
      }
      return stationMap;
    } catch (Exception ex) {
      ex.printStackTrace();
    } finally {

    }
    return null;
  }

  public Map<String, String> readFile3() {
    try {
      List<String> citys = FileUtils.readLines(new File("D:\\Users\\yincy\\Desktop\\火车票线路\\城市.txt"), "UTF-8");
      Map<String, String> cityMap = Maps.newHashMap();
      for (String city : citys) {
        String[] citysplit = city.split("\t");
        String trim = citysplit[1].replaceAll(" ", "").trim();
        String trim1 = citysplit[0].replaceAll(" ", "").trim();
        cityMap.put(trim, trim1);
      }
      return cityMap;
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

}
